package com.zhang.teachingcommunity_spring.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhang.teachingcommunity_spring.controller.resultType.Pagination;
import com.zhang.teachingcommunity_spring.controller.resultType.PostPage;
import com.zhang.teachingcommunity_spring.controller.resultType.getListType;
import com.zhang.teachingcommunity_spring.mapper.CommentMapper;
import com.zhang.teachingcommunity_spring.mapper.PostMapper;
import com.zhang.teachingcommunity_spring.mapper.SubjectMapper;
import com.zhang.teachingcommunity_spring.mapper.UserMapper;
import com.zhang.teachingcommunity_spring.mapper.entity.*;
import com.zhang.teachingcommunity_spring.service.PostService;
import com.zhang.teachingcommunity_spring.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("/forum")
public class PostController {
    @Autowired
    private PostService postService;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private SubjectMapper subjectMapper;
    @Autowired
    private PostMapper postMapper;
    @PostMapping("/getPostList")
    public Result getPostList(@RequestBody  PostPage postPage){
        IPage<Post> postList;
        String stuNo = postPage.getPost().getStuNo();
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("stu_no", stuNo);
        User user = userMapper.selectOne(userQueryWrapper);
        if(user == null) {
             postList = postService.getPostList(postPage.getPost(), postPage.getPage(), null);
            return Result.success("获取成功", postList);
        }
        postList = postService.getPostList(postPage.getPost(), postPage.getPage(), user.getUserId());
        return Result.success("获取成功", postList);
    }
    @PostMapping("/inset")
    public Result inset(@RequestBody  Post post){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:SS");
        Date date = new Date();
        String format = simpleDateFormat.format(date);
        post.setPublishTime(Timestamp.valueOf(format));
        postMapper.insert(post);
        return Result.success("插入成功", true);
    }
    @PostMapping("/update")
    public Result update(@RequestBody  Post post){
        postMapper.updateById(post);
        return Result.success("修改成功", true);
    }
    @GetMapping("/delete")
    public Result delete( Integer id){
        postMapper.deleteById(id);
        return Result.success("删除成功", true);
    }
    @GetMapping("/getDifferentSubjectInCountFn")
    private Result getDifferentSubjectInCount( ) {
        List<Map<String, Object>> differentSubjectInCount = postMapper.getDifferentSubjectInCountFn();
        return Result.success("获取成功",differentSubjectInCount);
    }
    @PostMapping("/getList")
    private Result getPostList(@RequestBody getListType getListType) {

        List<Map<String, Object>> postListCount = postMapper.getPostListCount(getListType.getSubjectId(), getListType.getSortNumber());
        List<Map<String,Object>> postList = postMapper.getPostList(getListType.getSubjectId(), getListType.getPagination(), getListType.getSortNumber());
        postList.forEach(item -> {
            Object userId = item.get("user_id");
            User user = userMapper.selectById((Serializable) userId);
            item.put("user", user);
        });
        HashMap<String, Object> list2 = new HashMap<>();
        getListType.getPagination().setTotal(postListCount.size());
        list2.put("postList",postList);
        list2.put("pagination", getListType.getPagination());
        return Result.success("获取成功",list2);
    }
    @GetMapping("/getHotSort")
    private Result getHotSort(Integer id) {
        List<Map<String, Object>> hotSort = postMapper.getHotSort();
        ArrayList<Integer> objects = new ArrayList<>();
        Map<String, Object> stringObjectMap = new HashMap<>();
        hotSort.forEach(item -> {
            Object tag = item.get("tag");
            if(!objects.contains(tag)){
                objects.add((Integer) tag);
            }
        });
        List<Subject> subjects = subjectMapper.selectBatchIds(objects);
        stringObjectMap.put("hotList",hotSort);
        stringObjectMap.put("subjects",subjects);
        return Result.success("获取成功",stringObjectMap);
    }
    @GetMapping("/getPostInfo")
    private Result getPostInfo(Integer id) {
        Post postInfo = postMapper.selectById(id);
        User user = userMapper.selectById(postInfo.getUserId());
        postInfo.setUser(user);
        return Result.success("获取成功",postInfo);
    }
}
